
import React from "react";
import {requireNativeComponent} from "react-native";
const PropTypes = require('prop-types');
const AndroidRefreshLayout = requireNativeComponent('RCTRefreshLayout', null);

export default class RefreshView extends React.Component {
    _refreshing = false;
    _loading = false;
    _nativeRef = undefined;
    static defaultProps = {
        refreshing: false,
        loading: false,
        refreshEnable:true,
        loadEnable:true
    };
    static propTypes = {
        refreshing: PropTypes.bool,
        loading: PropTypes.bool,
        onRefresh: PropTypes.func,
        onLoad: PropTypes.func,
        refreshEnable:PropTypes.bool,
        loadEnable:PropTypes.bool
    };

    componentDidMount() {
        this._refreshing = this.props.refreshing;
        this._loading = this.props.loading;
    }
    componentDidUpdate(prevProps: {refreshing: boolean,loading:boolean}) {
        if (this.props.refreshing !== prevProps.refreshing) {
            this._refreshing = this.props.refreshing;
        } else if (this.props.refreshing !== this._refreshing) {
            this._nativeRef.setNativeProps({refreshing: this.props.refreshing});
            this._refreshing = this.props.refreshing;
        }
        if (this.props.loading !== prevProps.loading) {
            this._loading = this.props.loading;
        } else if (this.props.loading !== this._loading) {
            this._nativeRef.setNativeProps({loading: this.props.loading});
            this._loading = this.props.loading;
        }
    }
    render(){
        return (
            <AndroidRefreshLayout
                {...this.props}
                ref={ref => {this._nativeRef = ref;}}
                onRefresh={() => {
                    this._refreshing = true;
                    this.props.onRefresh && this.props.onRefresh();
                    this.forceUpdate();
                }}
                onLoad={() => {
                    this._loading = true;
                    this.props.onLoad && this.props.onLoad();
                    this.forceUpdate();
                }}
            >
            </AndroidRefreshLayout>
        )
    }

    // _refreshing=(b)=>{
    //     UIManager.dispatchViewManagerCommand(
    //         ReactNative.findNodeHandle(this._nativeRef),
    //         UIManager.RCTRefreshLayout.Commands.refreshing,
    //         [b]
    //     );
    // };
    // _loading=(b)=>{
    //     UIManager.dispatchViewManagerCommand(
    //         ReactNative.findNodeHandle(this._nativeRef),
    //         UIManager.RCTRefreshLayout.Commands.loading,
    //         [b]
    //     );
    // };
}